package com.zhouyer.edu.utils;

import com.zhouyer.edu.annotation.MyAutoWired;
import com.zhouyer.edu.annotation.MyService;

import java.sql.SQLException;

/**
 * 进行事务管理：开启事务，提交事务，回滚事务
 *
 * @author zhouyer
 */
@MyService
public class TransactionManager {
    @MyAutoWired
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    /**
     * 开启事务：即设置手动提交事务
     *
     * @throws SQLException
     */
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConnection().setAutoCommit(false);
    }

    /**
     * 提交事务
     *
     * @throws SQLException
     */
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConnection().commit();
    }

    /**
     * 事务回滚
     */
    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConnection().rollback();
    }
}
